using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Presentation.Contracts;
using Presentation;

namespace ProjektZespolowy
{
    /// <summary>
    /// Add Forum Page.
    /// </summary>
    public partial class AddForum : Page, IAddForumView
    {
        #region Members

        /// <summary>
        /// Instance of presenter.
        /// </summary>
        private AddForumPresenter presenter;

        #endregion Members

        #region Override Methods

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter = new AddForumPresenter(this);
        }

        #endregion Override Methods

        #region IAddForumView Members

        /// <summary>
        /// Gets or sets the name of the forum.
        /// </summary>
        /// <value>The name of the forum.</value>
		public void ShowForums(System.Collections.Generic.ICollection<DataAccess.forum> forumList)
			{
			ViewState["tabelka"] = forumList;
			GridView1.DataSource = ViewState["tabelka"];
			GridView1.DataBind();
			}

		public void ShowComments(System.Collections.Generic.ICollection<DataAccess.comment> commentList)
			{
			
			}

		public string ForumName
        {
            get
            {
                return txtForumName.Text ;
            }
            set
            {
                txtForumName.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the forum description.
        /// </summary>
        /// <value>The forum description.</value>
        public string ForumDescription
        {
            get
            {
                return txtForumDescription.Text;
            }
            set
            {
                txtForumDescription.Text = value;
            }
        }

		public int LoggedUserID
			{
			get
				{
				if (Session[Globals.ContentNamesGlobals.SessionWithUserId] != null)
					return Int32.Parse(Session[Globals.ContentNamesGlobals.SessionWithUserId].ToString());
				else
					return -1;
				}
			}


        #endregion IAddForumView Members

        #region Private Methods

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {
			if (this.LoggedUserID > 0)
				presenter.Initialize();
			else
				Response.Redirect("~/Forum/Default.aspx");
            }
        }

        /// <summary>
        /// Calls presenter to add new forum.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void OnBtnAddForum(object sender, EventArgs e)
        {
            //add new forum
            presenter.AddNewForum();

            //initialize presenter
            presenter.Initialize();
        }

        #endregion Private Methods

		protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
			{
			HyperLink tmp = (HyperLink)GridView1.Rows[e.RowIndex].Cells[2].Controls[1];
			string a = tmp.NavigateUrl;
			string[] tab = a.Split('=');
			int forum_id = Int32.Parse(tab[1]);
			presenter.DeleteMyForum(forum_id);
			presenter.Initialize();
			}

		protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
			{
			GridView1.DataSource = ViewState["tabelka"];
			GridView1.PageIndex = e.NewPageIndex;
			GridView1.DataBind();
			}

    }
}
